package com.bweab.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * 这个过滤器解决了跨域问题
 *    也就是前后端通信问题
 * */
@WebFilter("/*")
public class CrossFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //解决跨域问题

        HttpServletRequest request =  (HttpServletRequest)servletRequest;
        System.out.println(request.getMethod());

        //设置响应信息
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //解决跨域问题

        response.setHeader("Access-Control-Allow-Origin","*");//设置所有域都可以跨域
        response.setHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS,HEAD,PUT,PATCH");//允许跨域的方法
        response.setHeader("Access-Control-Max-Age","36000");//设置跨域时间
        response.setHeader("Access-Control-Allow-Headers","Origin,X-Requested-With,Content-Type,Accept,Authorization,authorization");
        response.setHeader("Access-Control-Allow-Credentials","true");




        filterChain.doFilter(request,response);


    }
}
